<?php
/**
 * Created by PhpStorm.
 * User: jianglong
 * Date: 2017/11/10
 * Time: 下午3:38
 */

namespace app\common\log\turnCustomer;


/**
 * 移交客户
 */
use app\common\log\Base;
use app\manager\admin\ManagerException;
use think\db;

class TurnCustomer extends Base
{
    public function __construct(array $a_data)
    {
        parent::__construct();
        $this->a_data = $a_data;
    }

    /**
     * 移交一个
     */
    public function disOne()
    {
        $a_data = $this->a_data;
        try {
            //step1 判断客户经理
            $i_managerId = $a_data['manager_id'];
            if (empty($a_data['manager_id']) || empty(Db::name('manager')->find(['id' => $i_managerId]))):
                throw new ManagerException('客户经理不存在！');
            endif;
            //step2 判断客户
            if (empty($a_data['customer_id'])):
                throw new ManagerException('客户不存在！');
            endif;
            $a_customer = Db::name('customer')->find(['id' => $a_data['customer_id']]);
            if ($a_customer['manager_id'] == $i_managerId):
                throw new ManagerException('移交客户经理与现经理相同！');
            endif;
            //查找当前客户是否存在未被处理的移交
            if (Db::name('customer_turn_log')->where([
                'customer_id' => $a_customer['id'],
                'status' => 0,
                'next_log' => 0
            ])->find()):
                throw new ManagerException('客户正处于移交状态，请等待移交处理！');
            endif;
            if (empty($a_customer)):
                throw new ManagerException('客户不存在！');
            endif;
            //超级管理员或领导直接移交
            $a_insertData = [
                'manager_id' => $i_managerId,
                'old_manager' => $a_customer['manager_id'],
                'customer_id' => $a_data['customer_id'],
                'start_time' => $_SERVER['REQUEST_TIME'],
                'message' => $a_data['message'],
                'time' => 0,
                'operate_id' => $this->i_uid,
                'next_log' => 0,
                'status' => 0,
            ];
            if (ROLE_ID == 2 || ROLE_ID == 1):
                //更改用户所属
                if (!Db::name('customer')->where('id', $a_data['customer_id'])->update(['manager_id' => $i_managerId, 'turn_status' => 1])):
                    throw new ManagerException("移交客户失败");
                endif;
                $a_insertData['time'] = $_SERVER['REQUEST_TIME'];
                $a_insertData['status'] = 1;
            else:
                if (!Db::name('customer')->where('id', $a_data['customer_id'])->update(['turn_status' => 0])):
                    throw new ManagerException("申请移交客户失败");
                endif;
            endif;
            //移交成功
            $o_model = Db::name('customer_turn_log');
            $o_model->insert($a_insertData);

            return true;
        } catch (ManagerException $e) {
            //移交失败
            /*            $a_insertData = [
                            'time' => $_SERVER['REQUEST_TIME'],
                            'message' => $a_data['message'],
                            'error' => $e->getErrorMsg(),
                            'operate_id' => $this->i_uid,
                            'status' => 0,
                        ];
                        $o_model = Db::name('customer_turn_log');
                        $o_model->insert($a_insertData);*/
            return $e->getErrorMsg();
        }
    }

}